/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package vistas.buscar;

import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import persistencia.Inmueble;

/**
 *
 * @author hellwolf
 */
public class buscarInmueble extends javax.swing.JFrame {

    private String id = "where id like '%";
    private String calle = "where calle like '%";
    private String importe = "where importe like '%";
    private String telefono = "where telefono like '%";
    private String dninif = "where dninif like '%";
    private String metrosCudrados = "where metrosCuadrados like '%";
    private String fin = "%'";
    private String sql = "select * from inmueble ";
    private String sqlReseteo = "select * from inmueble ";
    /**
     * Creates new form buscarInmueble
     */
    public buscarInmueble() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        arrendamientoPUEntityManager = java.beans.Beans.isDesignTime() ? null : javax.persistence.Persistence.createEntityManagerFactory("arrendamientoPU").createEntityManager();
        jLabel1 = new javax.swing.JLabel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Buscar Inmueble");

        jLabel1.setText("Buscar por: ");

        jComboBox1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Id", "Calle o Numero", "Importe", "Telefono", "Metros Cuadrados", " " }));

        jLabel2.setText("Dato a buscar:");

        jButton1.setText("Buscar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {

            }
        ));
        jScrollPane1.setViewportView(jTable1);

        jLabel3.setText("Resultados Encontrados: ");

        jLabel4.setText("0");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, 146, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jLabel2)
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel3)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jLabel4)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 223, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(jButton1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jComboBox1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jLabel4))
                .addGap(10, 10, 10)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 291, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        String opcionBusqueda = (String) this.jComboBox1.getSelectedItem();
        List<Inmueble> resultados = new ArrayList<Inmueble>();
        resultados.clear();
        if (opcionBusqueda.compareTo("Id") == 0) {
            this.sql = this.sql + this.id + this.jTextField1.getText() + this.fin;
            System.out.println(sql);
            resultados = this.arrendamientoPUEntityManager.createNativeQuery(sql, Inmueble.class).getResultList();
            cargarTabla(resultados);
            this.jLabel4.setText(String.valueOf(resultados.size()));
        } else if (opcionBusqueda.compareTo("Calle o Numero") == 0) {
            this.sql = this.sql + this.calle + this.jTextField1.getText() + this.fin;
            System.out.println(sql);
            resultados = this.arrendamientoPUEntityManager.createNativeQuery(sql, Inmueble.class).getResultList();
            cargarTabla(resultados);
            this.jLabel4.setText(String.valueOf(resultados.size()));
        } else if (opcionBusqueda.compareTo("Metros Cuadrados") == 0) {
            this.sql = this.sql + this.metrosCudrados + this.jTextField1.getText() + this.fin;
            System.out.println(sql);
            resultados = this.arrendamientoPUEntityManager.createNativeQuery(sql, Inmueble.class).getResultList();
            cargarTabla(resultados);
            this.jLabel4.setText(String.valueOf(resultados.size()));
        } else if (opcionBusqueda.compareTo("Importe") == 0) {
            this.sql = this.sql + this.importe + this.jTextField1.getText() + this.fin;
            System.out.println(sql);
            resultados = this.arrendamientoPUEntityManager.createNativeQuery(sql, Inmueble.class).getResultList();
            cargarTabla(resultados);
            this.jLabel4.setText(String.valueOf(resultados.size()));
        } else if (opcionBusqueda.compareTo("Telefono") == 0) {
            this.sql = this.sql + this.telefono + this.jTextField1.getText() + this.fin;
            System.out.println(sql);
            resultados = this.arrendamientoPUEntityManager.createNativeQuery(sql, Inmueble.class).getResultList();
            cargarTabla(resultados);
            this.jLabel4.setText(String.valueOf(resultados.size()));
        } 
        this.sql = this.sqlReseteo;
    }//GEN-LAST:event_jButton1ActionPerformed

    
    public void cargarTabla(List<Inmueble> array) {
        DefaultTableModel tabla = new DefaultTableModel();
        tabla.addColumn("Id");
        tabla.addColumn("Calle");
        tabla.addColumn("Metros Cuadrados");
        tabla.addColumn("Importe");
        tabla.addColumn("Telefono");
        tabla.setRowCount(array.size());

        int i = 0;
        for (Inmueble in : array) {
            tabla.setValueAt(in.getId(), i, 0);
            tabla.setValueAt(in.getCalle(), i, 1);
            tabla.setValueAt(in.getMetrosCuadrados(), i, 2);
            tabla.setValueAt(in.getImporte(), i, 3);
            tabla.setValueAt(in.getTelefono(), i, 4);
            i++;
        }
        this.jTable1.setModel(tabla);
    }
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(buscarInmueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(buscarInmueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(buscarInmueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(buscarInmueble.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                new buscarInmueble().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.persistence.EntityManager arrendamientoPUEntityManager;
    private javax.swing.JButton jButton1;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTable1;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables
}
